package com.cwiyc.orderonlineback.service;

import com.cwiyc.orderonlineback.common.Result;
import com.cwiyc.orderonlineback.entity.*;
import com.cwiyc.orderonlineback.mapper.MealMapper;
import com.cwiyc.orderonlineback.mapper.OrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderServiceImpl {
    @Resource
    OrderMapper orderMapper;
    @Resource
    MealMapper mealMapper;

    public Result<?> createOrder(@RequestBody Order order) {
        int res = orderMapper.createorder(order.getOrealid(), order.getOcreatetime(), order.getOtotalprice(), order.getOstatus(), order.getOuid());
        if (res == 0) {
            return Result.error("-1", "创建订单失败");
        }
        Order ress = orderMapper.getorder(order.getOrealid());
        if (ress == null) {
            return Result.error("-1", "获取订单信息失败");
        }
        return Result.success(ress);
    }

    public Result<?> createOd(@RequestBody List<Orderdetail> orderdetails) {
        for (int i = 0; i < orderdetails.size(); i++) {
            int res = orderMapper.createOd(orderdetails.get(i).getOdmid(), orderdetails.get(i).getOdmcount(), orderdetails.get(i).getOdoid());
            if (res == 0) {
                return Result.error("-1", "创建订单详情失败");
            }
            int resss =mealMapper.updatess(orderdetails.get(i).getOdmid());
            if (resss  == 0) {
                return Result.error("-1", "更新菜品库存销量失败");
            }
        }
        List<Orderdetail> ress = orderMapper.getorderdetail(orderdetails.get(0).getOdoid());
        if (ress == null) {
            return Result.error("-1", "获取订单详情失败");
        }
        return Result.success(ress);
    }

    public Result<?> getOrderlist(@RequestBody Integer uid) {
        List<Orderlist> res = orderMapper.getorderlist(uid);
        if (res == null) {
            return Result.error("-1", "获取订单列表失败");
        }
        return Result.success(res);
    }

    public Result<?> getOrderlistdetail(@RequestBody Integer uid) {
        List<Orderlistdetail> res = orderMapper.getorderlistdetail(uid);
        if (res == null) {
            return Result.error("-1", "获取订单列表详情失败");
        }
        return Result.success(res);
    }

    public Result<?> userOrderlist(@RequestBody Integer uid) {
        List<Orderlist> res = orderMapper.userorderlist(uid);
        if (res == null) {
            return Result.error("-1", "获取订单列表失败");
        }
        return Result.success(res);
    }


    public Result<?> userOrderlistdetail(@RequestBody Integer uid) {
        List<Orderlistdetail> res = orderMapper.userorderlistdetail(uid);
        if (res == null) {
            return Result.error("-1", "获取订单列表详情失败");
        }
        return Result.success(res);
    }

    public Result<?> operateOrder(@RequestBody Orderoperate orderoperate) {
        int res = orderMapper.operateorder(orderoperate.getOid(),orderoperate.getPayload(),orderoperate.getFinishtime());
        if (res == 0) {
            return Result.error("-1", "操作订单失败");
        }
        return Result.success();
    }
}
